package cn.edu.nuc.onlinestore.vo;

import java.io.File;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import cn.edu.nuc.onlinestore.model.Goods;
import cn.edu.nuc.onlinestore.stream.ObjectStream;

public class goodsDo implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = -5461830533930123940L;
	private int num;
	private double price;
	private String goodsName; 
	private int gid;
	private String msg;
	private String jianjie;
	public String getMsg() {
		return msg;
	}
	public goodsDo(){}
	public goodsDo(int gid, String goodsName, double price, int num,String jianjie){
		File file=new File("D:/store/goods");
		if(!file.exists()){
			file.mkdir();
		}
		this.gid=gid;
		this.goodsName=goodsName;
		this.price=price;
		this.num=num;
		this.jianjie=jianjie;
	}
	/**
	 * 对商品进行添加
	 * @return
	 */
	public boolean goodsAdd(){
		File file=new File("D:/store/goods/"+goodsName+".txt");
		if(file.exists()){
			msg="此商品已存在";
			return false;
		}else{
			int id=0;
			if(getAllFiles()==null){id=0;}
			else{id=getAllFiles().length;}
			ObjectStream.write("d:/store/goods/"+goodsName+".txt",new Goods(id,goodsName,price,num,jianjie));
			msg="商品添加成功,您添加了:"+num+"件"+goodsName+",它的单价为:"+price+"。"+"简介是"+jianjie;
			return true;
		}
	}
	public File[] getAllFiles(){
		File file=new File("D:/store/goods");
		File[] goodlist=file.listFiles();
		return goodlist;
	}
	/**
	 * 修改商品数量、简介
	 * @return
	 */
	public boolean goodsUpdate(int newnum,String jianjie){
		Goods good=ObjectStream.read(Goods.class,"d:/store/goods/"+goodsName+".txt");
		good.setJianjie(jianjie);
		good.setNum(newnum);
		ObjectStream.write("goods/"+goodsName+".txt",good);
		msg="更改数量成功"+good.getGoodsName()+"现有"+good.getNum()+"件,简介为"+good.getJianjie();
		return true;
	}
	/**
	 * 显示购物列表
	 * @return
	 */
	public Map<String, Goods> showGoodList(){
		Map<String,Goods> goodlist=new HashMap<>();
		for(File file:getAllFiles()){
			Goods good=ObjectStream.read(Goods.class,file.getAbsolutePath());
			goodlist.put(good.getGoodsName(), good);
		}
		return goodlist;
	}
	
	public Boolean removegood(){
		File file=new File("D:/store/goods/"+goodsName+".txt");
		file.delete();
		msg="删除成功！,"+"共删除"+num+"件"+goodsName+"商品";
		return true;
	}
	/**
	 * 利用file实现名称查询
	 * @return
	 */
	public Goods serachname(String name){
		Set<Entry<String, Goods>> namelist=showGoodList().entrySet();
		for(Entry<String, Goods> names:namelist){
			if(name.equals(names)){
				this.msg="找到搜索结果如下";
				return showGoodList().get(name);
			}
		}
		this.msg="未找到结果";
		return null;
	}
	/**>
	 * 利用file实现id查询
	 * @return
	 */
	public Goods serachid(int id){
		this.gid=id;
		Set<Entry<String, Goods>> namelist=showGoodList().entrySet();
		for(Entry<String, Goods> names:namelist){
			if(showGoodList().get(names).getGid()==gid){
				this.msg="找到搜索结果如下";
				return showGoodList().get(names);
			}
		}
		this.msg="未找到结果";
		return null;
	}
	/*public static void main(String args[]){
		new goodsDo(1,"水杯",15.00,200).goodsAdd();
		new goodsDo(2,"水瓶",35.00,200).goodsAdd();
		new goodsDo(3,"天堂伞",55.00,200).goodsAdd();
		new goodsDo(4,"男袜",8.00,200).goodsAdd();
		new goodsDo(5,"农夫山泉",2.00,200).goodsAdd();
		new goodsDo(6,"毛巾",9.90,200).goodsAdd();
		new goodsDo(7,"牙刷",15.00,200).goodsAdd();
		new goodsDo(8,"洗发水",15.00,200).goodsAdd();
		new goodsDo(9,"牙膏",15.00,200).goodsAdd();
		new goodsDo(10,"海尔全自动洗衣机",2699.00,200).goodsAdd();
	}*/
}

